package com.ph.irp.sell.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.dto.PageDTO;
import com.ph.irp.dto.SellOrderDTO;
import com.ph.irp.model.PSellSellorder;
import com.ph.irp.model.PSellSellorderdetail;
import com.ph.irp.sell.service.SellOrderService;
import com.ph.util.DateUtil;
import com.ph.util.pagination.PageUtil;

public class SellOrderAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//销售订单model1
	private PSellSellorder sellOrderModel = null ;
	//销售订单service2
	private SellOrderService sellOrderService;
	private SellOrderDTO sellOrderDTO=null;
	private List<SellOrderDTO> sellOrderDTOList=null;
	 //临时销售订单list
	private List<SellOrderDTO> tempsellOrderList ;
	//物料id数组3
	private String[] materialID;
	//计量单位数组4
	private String[] measureUnitId;
	//基本单位数量5
	private String[] basicUnitAmount;
	//数量数组6
	private String[] amount;
	//单价数组7
	private String[] simplePrice;
	//含税单价数组8
	private String[] includeTaxPrice;
	//金额9
	private String[] money;
	//税率10
	private String[] taxPercent;
	//折扣率11
	private String[] discount;
	//折扣额12
	private String[] discountPrice;
	//实际含税单价13
	private String[] acctualIncludeTaxPrice;
	//销项税额selltaxmoney14
	private String[] sellTaxMoney;
	//价税合计totalmoney15
	private String[] totalMoney;
	//备注16
	private String[] deliveryDate;
	//备注数组17
	private String[] memo;
	//源单单号18
	private String[] sourceBillCode;
	private HttpServletRequest request;
	private String beginDate;
	private String endDate;
	//分页信息
	private String cpage;
	
	private PageDTO page;
  
	/**
	 * 打开新增页面
	 * @return
	 */
	public String showAddSellOrder() throws Throwable{
		this.sellOrderModel = null;
		sellOrderModel = new PSellSellorder();
		Date currentDate = new Date();
		String stringDate =  DateUtil.date2String(currentDate,"yyyy-MM-dd");
		request = ServletActionContext.getRequest();
		String makeBiller = (String) request.getSession().getAttribute("userName");
		sellOrderModel.setCreateDate(stringDate);
		sellOrderModel.setSettleDate(stringDate);
		sellOrderModel.setMakeBiller(makeBiller);
		return "success";
	}

	/**
	 * 销售订单新增
	 * @return
	 * @throws Throwable 
	 */
	public String addSellOrder() throws Throwable{
		List<PSellSellorderdetail> sellOrderDetailLists = new ArrayList<PSellSellorderdetail>();
		if(materialID != null){
		for(int i=0;i<materialID.length;i++){
			PSellSellorderdetail sellOrderDetailModel = new PSellSellorderdetail();
			sellOrderDetailModel.setMaterialID(Integer.parseInt(materialID[i]));
			sellOrderDetailModel.setMeasureUnitId(Integer.parseInt(measureUnitId[i]));
			sellOrderDetailModel.setBasicUnitAmount(Float.parseFloat(basicUnitAmount[i]));
			sellOrderDetailModel.setAmount(Float.parseFloat(amount[i]));
			sellOrderDetailModel.setSimplePrice(Float.parseFloat(simplePrice[i]));
			sellOrderDetailModel.setIncludeTaxPrice(Float.parseFloat(includeTaxPrice[i]));
			sellOrderDetailModel.setMoney(Float.parseFloat(money[i]));
			sellOrderDetailModel.setTaxPercent(Float.parseFloat(taxPercent[i]));
			sellOrderDetailModel.setDiscount(Float.parseFloat(discount[i]));
			sellOrderDetailModel.setDiscountPrice(Float.parseFloat(discountPrice[i]));
			sellOrderDetailModel.setAcctualIncludeTaxPrice(Float.parseFloat(acctualIncludeTaxPrice[i]));
			sellOrderDetailModel.setSellTaxMoney(Float.parseFloat(sellTaxMoney[i]));
			sellOrderDetailModel.setTotalMoney(Float.parseFloat(totalMoney[i]));
			sellOrderDetailModel.setDeliveryDate((deliveryDate[i]));
			sellOrderDetailModel.setMemo(memo[i]);
			sellOrderDetailModel.setSourceBillCode(sourceBillCode[i]);
			sellOrderDetailModel.setOutStorageAmountBasic(0f);
			sellOrderDetailModel.setOutStorageAmount(0f);
			sellOrderDetailLists.add(sellOrderDetailModel);
			
			}
		this.sellOrderService.addSellOrder(this.sellOrderModel, sellOrderDetailLists);
		}
		
		sellOrderModel = null;
		sellOrderDetailLists = null;
	
		return "success";
	}
	/**
	 * 打开销售订单List页面
	 * @return
	 * @throws Throwable 
	 */
	public String showSellOrderList() throws Throwable{
		tempsellOrderList = this.sellOrderService.getAllSellOrder();
		page = PageUtil.generatePageDTO(tempsellOrderList,
				"showSellOrderList.action", cpage);
		sellOrderDTOList = page.getResList() ;
		return "success";
	}
	/**
	 * 根据条件查询销售订单
	 * @return
	 * @throws Throwable
	 */
	public String getSellOrderByCondition() throws Throwable{
		sellOrderDTOList = this.sellOrderService.getSellOrderByCondition(sellOrderModel,beginDate,endDate);
		sellOrderModel = null;
		sellOrderDTO=null;
		return "success";
	}

	public PSellSellorder getSellOrderModel() {
		return sellOrderModel;
	}

	public void setSellOrderModel(PSellSellorder sellOrderModel) {
		this.sellOrderModel = sellOrderModel;
	}

	public SellOrderService getSellOrderService() {
		return sellOrderService;
	}

	public void setSellOrderService(SellOrderService sellOrderService) {
		this.sellOrderService = sellOrderService;
	}

	public SellOrderDTO getSellOrderDTO() {
		return sellOrderDTO;
	}

	public void setSellOrderDTO(SellOrderDTO sellOrderDTO) {
		this.sellOrderDTO = sellOrderDTO;
	}

	public List<SellOrderDTO> getSellOrderDTOList() {
		return sellOrderDTOList;
	}

	public void setSellOrderDTOList(List<SellOrderDTO> sellOrderDTOList) {
		this.sellOrderDTOList = sellOrderDTOList;
	}

	public List<SellOrderDTO> getTempsellOrderList() {
		return tempsellOrderList;
	}

	public void setTempsellOrderList(List<SellOrderDTO> tempsellOrderList) {
		this.tempsellOrderList = tempsellOrderList;
	}

	public String[] getMaterialID() {
		return materialID;
	}

	public void setMaterialID(String[] materialID) {
		this.materialID = materialID;
	}

	public String[] getMeasureUnitId() {
		return measureUnitId;
	}

	public void setMeasureUnitId(String[] measureUnitId) {
		this.measureUnitId = measureUnitId;
	}

	public String[] getBasicUnitAmount() {
		return basicUnitAmount;
	}

	public void setBasicUnitAmount(String[] basicUnitAmount) {
		this.basicUnitAmount = basicUnitAmount;
	}

	public String[] getAmount() {
		return amount;
	}

	public void setAmount(String[] amount) {
		this.amount = amount;
	}

	public String[] getSimplePrice() {
		return simplePrice;
	}

	public void setSimplePrice(String[] simplePrice) {
		this.simplePrice = simplePrice;
	}

	public String[] getIncludeTaxPrice() {
		return includeTaxPrice;
	}

	public void setIncludeTaxPrice(String[] includeTaxPrice) {
		this.includeTaxPrice = includeTaxPrice;
	}

	public String[] getMoney() {
		return money;
	}

	public void setMoney(String[] money) {
		this.money = money;
	}

	public String[] getTaxPercent() {
		return taxPercent;
	}

	public void setTaxPercent(String[] taxPercent) {
		this.taxPercent = taxPercent;
	}

	public String[] getDiscount() {
		return discount;
	}

	public void setDiscount(String[] discount) {
		this.discount = discount;
	}

	public String[] getDiscountPrice() {
		return discountPrice;
	}

	public void setDiscountPrice(String[] discountPrice) {
		this.discountPrice = discountPrice;
	}

	public String[] getAcctualIncludeTaxPrice() {
		return acctualIncludeTaxPrice;
	}

	public void setAcctualIncludeTaxPrice(String[] acctualIncludeTaxPrice) {
		this.acctualIncludeTaxPrice = acctualIncludeTaxPrice;
	}

	public String[] getSellTaxMoney() {
		return sellTaxMoney;
	}

	public void setSellTaxMoney(String[] sellTaxMoney) {
		this.sellTaxMoney = sellTaxMoney;
	}

	public String[] getTotalMoney() {
		return totalMoney;
	}

	public void setTotalMoney(String[] totalMoney) {
		this.totalMoney = totalMoney;
	}

	public String[] getDeliveryDate() {
		return deliveryDate;
	}

	public void setDeliveryDate(String[] deliveryDate) {
		this.deliveryDate = deliveryDate;
	}

	public String[] getMemo() {
		return memo;
	}

	public void setMemo(String[] memo) {
		this.memo = memo;
	}

	public String[] getSourceBillCode() {
		return sourceBillCode;
	}

	public void setSourceBillCode(String[] sourceBillCode) {
		this.sourceBillCode = sourceBillCode;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	
}
